using System;
using System.IO;
using System.Data;
using System.Collections;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing.Imaging;

namespace BEGalleryExtension
{
    [Themeable(true)]
    public class PagerControl : WebControl
    {
        #region Properties

        private string firstImageURL;
        public string FirstImageURL
        {
            get { return firstImageURL; }
            set { firstImageURL = value; }
        }

        private string firstText;
        public string FirstText
        {
            get { return firstText; }
            set { firstText = value; }
        }

        private string lastImageURL;
        public string LastImageURL
        {
            get { return lastImageURL; }
            set { lastImageURL = value; }
        }

        private string lastText;
        public string LastText
        {
            get { return lastText; }
            set { lastText = value; }
        }

        private string nextImageURL;
        public string NextImageURL
        {
            get { return nextImageURL; }
            set { nextImageURL = value; }
        }

        private string nextText;
        public string NextText
        {
            get { return nextText; }
            set { nextText = value; }
        }

        private string prevImageURL;
        public string PrevImageURL
        {
            get { return prevImageURL; }
            set { prevImageURL = value; }
        }

        private string prevText;
        public string PrevText
        {
            get { return prevText; }
            set { prevText = value; }
        }

        private PagedDataSource pagedData;
        public PagedDataSource PagedData
        {
            get { return pagedData; }
            set { pagedData = value; }
        }

        #endregion

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
        }

        public override void RenderControl(HtmlTextWriter writer)
        {
            if (pagedData != null)
                if (pagedData.PageCount == 1)
                    return;

            Page thePage = HttpContext.Current.Handler as Page;
            if (thePage != null)
            {
                this.firstImageURL = thePage.ClientScript.GetWebResourceUrl(typeof(PagerControl), "BEGalleryExtension.Images.resultset_first.png");
                this.firstText = "<< First";
                this.prevImageURL = thePage.ClientScript.GetWebResourceUrl(typeof(PagerControl), "BEGalleryExtension.Images.resultset_previous.png");
                this.prevText = "< Prev";
                this.nextImageURL = thePage.ClientScript.GetWebResourceUrl(typeof(PagerControl), "BEGalleryExtension.Images.resultset_next.png");
                this.NextText = "Next >";
                this.lastImageURL = thePage.ClientScript.GetWebResourceUrl(typeof(PagerControl), "BEGalleryExtension.Images.resultset_last.png");
                this.lastText = "Last >>";
            }

            PlaceHolder pages = new PlaceHolder();
            HyperLink first = new HyperLink();
            first.Text = FirstText;
            first.ImageUrl = FirstImageURL;

            HyperLink prev = new HyperLink();
            prev.Text = PrevText;
            prev.ImageUrl = PrevImageURL;

            Literal current = new Literal();
            HyperLink next = new HyperLink();
            next.Text = NextText;
            next.ImageUrl = NextImageURL;

            HyperLink last = new HyperLink();
            last.Text = LastText;
            last.ImageUrl = LastImageURL;

            current.Text = "<b> Page " + pagedData.CurrentPageIndex + " </b>";
            QueryString query = new QueryString();
            query.RemoveParameter("pg");

            if (pagedData.CurrentPageIndex > 0)
                first.NavigateUrl = query.All;

            if (pagedData.CurrentPageIndex > 0)
            {
                query["pg"] = Convert.ToString(pagedData.CurrentPageIndex - 1);
                prev.NavigateUrl = query.All;
            }

            if (pagedData.CurrentPageIndex < PagedData.PageCount - 1)
            {
                query["pg"] = Convert.ToString(pagedData.CurrentPageIndex + 1);
                next.NavigateUrl = query.All;
            }

            if (pagedData.CurrentPageIndex < PagedData.PageCount - 1)
            {
                query["pg"] = Convert.ToString(pagedData.PageCount - 1);
                last.NavigateUrl = query.All;
            }

            int start = (pagedData.CurrentPageIndex / 10) * 10;
            int end = start + 10;
            int max = pagedData.PageCount - 1;

            if (start > 0)
            {
                HyperLink pagePrevTen = new HyperLink();
                pagePrevTen.Text = "[ " + (start - 10) + " - " + (end - 11) + " ]";
                query["pg"] = Convert.ToString(end - 11);
                pagePrevTen.NavigateUrl = query.All;
                pages.Controls.Add(pagePrevTen);
            }

            for (int x = start; x < end; x++)
            {
                if (x <= max)
                {
                    if (PagedData.CurrentPageIndex == x)
                    {
                        Label thePageCurrent = new Label();
                        thePageCurrent.Text = "[" + x + "]";
                        pages.Controls.Add(thePageCurrent);
                    }
                    else
                    {
                        HyperLink thePageLink = new HyperLink();
                        thePageLink.Text = "[" + x + "]";
                        query["pg"] = Convert.ToString(x);
                        thePageLink.NavigateUrl = query.All;
                        pages.Controls.Add(thePageLink);
                    }
                }
            }

            if (end < max)
            {
                HyperLink thePageNextTen = new HyperLink();
                thePageNextTen.Text = "[" + (start + 10) + " - " + (end + 10) + "]";
                query["pg"] = Convert.ToString(start + 10);
                thePageNextTen.NavigateUrl = query.All;
                pages.Controls.Add(thePageNextTen);
            }

            textRenderControl(first, writer);
            textRenderControl(prev, writer);
            textRenderControl(current, writer);
            textRenderControl(next, writer);
            textRenderControl(last, writer);
            textRenderControl(Constants.LineBreak, writer);
            textRenderControl(pages, writer);
        }

        public void textRenderControl(Control ctl, HtmlTextWriter writer)
        {
            StringWriter sw = new StringWriter();
            ctl.RenderControl(new HtmlTextWriter(sw));
            writer.Write(sw.ToString());
            writer.Write(Environment.NewLine);
        }

        public void SetPagedDataSource(IEnumerable dataSource, int pageSize, int currentPage)
        {
            pagedData = new PagedDataSource();
            pagedData.AllowPaging = true;
            pagedData.DataSource = dataSource;
            pagedData.PageSize = pageSize;

            if (currentPage > pagedData.PageCount - 1)
                currentPage = pagedData.PageCount - 1;

            if (currentPage < 0)
                currentPage = 0;

            pagedData.CurrentPageIndex = currentPage;
        }
    }
}
